"""
集成测试初始化文件
"""

import pytest
import sys
import os
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(project_root))

# 确保可以导入所有需要测试的模块
try:
    from widgets.calendar import models, services, utils, schemas, router
    from main import app
    print("✅ 集成测试模块导入成功")
except ImportError as e:
    print(f"❌ 集成测试模块导入失败: {e}")
    sys.exit(1)

# 集成测试配置
def pytest_configure(config):
    """pytest配置"""
    config.addinivalue_line(
        "markers", "integration: mark test as integration test"
    )
    config.addinivalue_line(
        "markers", "slow: mark test as slow running"
    )
    config.addinivalue_line(
        "markers", "database: mark test as database test"
    )

# 集成测试固件
@pytest.fixture(scope="session")
def integration_test_app():
    """集成测试应用"""
    from fastapi import FastAPI
    from widgets.calendar import router
    
    app = FastAPI()
    app.include_router(router)
    return app

@pytest.fixture(scope="session")
def test_client():
    """测试客户端"""
    from fastapi.testclient import TestClient
    from main import app
    
    return TestClient(app)

@pytest.fixture(scope="session")
def auth_headers():
    """认证头"""
    return {
        "Authorization": "Bearer test-token",
        "Content-Type": "application/json"
    }

@pytest.fixture(scope="session")
def test_user_data():
    """测试用户数据"""
    return {
        "id": 1,
        "email": "test@example.com",
        "name": "测试用户",
        "is_active": True
    }

@pytest.fixture(scope="session")
def test_calendar_data():
    """测试日历数据"""
    return {
        "name": "测试日历",
        "description": "这是一个测试日历",
        "color": "#007bff",
        "timezone": "Asia/Shanghai",
        "visibility": "private",
        "default_permission": "view",
        "is_default": False
    }

@pytest.fixture(scope="session")
def test_event_data():
    """测试日程数据"""
    from datetime import datetime, timedelta
    
    now = datetime.now()
    return {
        "title": "测试日程",
        "description": "这是一个测试日程",
        "location": "测试地点",
        "start_time": now + timedelta(days=1),
        "end_time": now + timedelta(days=1, hours=1),
        "all_day": False,
        "timezone": "Asia/Shanghai",
        "status": "confirmed",
        "priority": "medium",
        "visibility": "private",
        "calendar_id": 1
    }

@pytest.fixture(scope="session")
def test_workflow_data():
    """测试工作流数据"""
    from datetime import datetime, timedelta
    
    now = datetime.now()
    return {
        "calendar": {
            "name": "工作日历",
            "description": "我的工作日程",
            "color": "#007bff",
            "timezone": "Asia/Shanghai",
            "visibility": "private",
            "default_permission": "view",
            "is_default": False
        },
        "event": {
            "title": "团队会议",
            "description": "每周团队例会",
            "location": "会议室A",
            "start_time": now + timedelta(days=1),
            "end_time": now + timedelta(days=1, hours=1),
            "all_day": False,
            "timezone": "Asia/Shanghai",
            "status": "confirmed",
            "priority": "medium",
            "visibility": "private",
            "calendar_id": 1
        },
        "recurrence": {
            "repeat_type": "weekly",
            "interval": 1,
            "repeat_days": [1, 2, 3, 4, 5],
            "end_date": now + timedelta(days=90)
        },
        "reminder": {
            "reminder_type": "popup",
            "minutes_before": 15,
            "message": "团队会议即将开始"
        },
        "share": {
            "shared_with_email": "teammate@example.com",
            "permission": "edit"
        }
    }